﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using Fluid.Controls;
using TouchRC.Classes;
using System.Drawing;
using TouchRC.Logic;

namespace TouchRC.Custom_Controls
{
    public class ProgressBar : FluidLabel
    {
        #region Private Members

        private TimeSpan m_CurrentTime = TimeSpan.Zero;
        private TimeSpan m_TotalTime = TimeSpan.Zero;
        #endregion

        public void SetNewValues(TimeSpan currentTime, TimeSpan totalTime)
        {
            m_CurrentTime = currentTime;
            m_TotalTime = totalTime;
            Refresh();
        }

        private int GetWidth()
        {
            int retVal = 0;
            if (m_CurrentTime != null && m_TotalTime != null)
            {
                double percentage = m_CurrentTime.TotalSeconds / m_TotalTime.TotalSeconds;
                retVal = (int)(this.Width * percentage);
            }

            return retVal;
        }

        public override void OnPaint(FluidPaintEventArgs e)
        {
            e.Graphics.FillRectangle(new SolidBrush(Color.White), this.Bounds.X, this.Bounds.Y,
                             this.Width, this.Height);
            e.Graphics.FillRectangle(new SolidBrush(Color.Black), this.Bounds.X + 1, this.Bounds.Y + 1,
                             this.Width - 2, this.Height - 2);
            e.Graphics.FillRectangle(new SolidBrush(Color.White), this.Bounds.X, this.Bounds.Y,
                                     GetWidth(), this.Height);
        }

        public override bool Active { get { return true; } }

        public override bool OnClick(PointEventArgs e)
        {
            double percentage = (double)e.X / (double)this.Width;
            TimeSpan newCurrent = TimeSpan.FromSeconds((int)(percentage * m_TotalTime.TotalSeconds));

            VmcControllerRemote remote = new VmcControllerRemote(MainMenuPanel.CurrentComputer);
            remote.JumpToTime(newCurrent);
            remote.PressButton(RemoteCommands.ButtonCommands.Play);

            return true;
        }
    }
}
